package edu.arhs.team1100.aerialassist.input;

import edu.arhs.team1100.aerialassist.handlers.EventHandler;
import edu.arhs.team1100.aerialassist.handlers.TeamEventMatchHandler;
import edu.arhs.team1100.aerialassist.scouting.objects.TeamEventMatch;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Eddie
 */
public class TeamEventMatchViewer extends javax.swing.JFrame {

    /**
     * Creates new form TeamEventMatchViewer
     */
    public TeamEventMatchViewer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sortingButtonGroup = new javax.swing.ButtonGroup();
        matchScrollPanel = new javax.swing.JScrollPane();
        matchTable = new javax.swing.JTable();
        addButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        updateButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        teamButton = new javax.swing.JButton();
        searchTabbedPane = new javax.swing.JTabbedPane();
        autonomousPanel = new javax.swing.JPanel();
        autoBallCountLabel = new javax.swing.JLabel();
        autoBallCountSlider = new javax.swing.JSlider();
        zoneChangeCheckBox = new javax.swing.JCheckBox();
        autoBallGoalLabel = new javax.swing.JLabel();
        autoBallGoalSlider = new javax.swing.JSlider();
        ableToUnloadAutoBallCheckBox = new javax.swing.JCheckBox();
        teleopScrollPane = new javax.swing.JScrollPane();
        teleopPanel = new javax.swing.JPanel();
        ableToTrussTossCheckBox = new javax.swing.JCheckBox();
        floorPickupSlider = new javax.swing.JSlider();
        ableToTrussCatchCheckBox = new javax.swing.JCheckBox();
        ableToCatchCheckBox = new javax.swing.JCheckBox();
        floorPickupLabel = new javax.swing.JLabel();
        highGoalAccuracySlider = new javax.swing.JSlider();
        ballsCaughtFromHPAccuracyLabel = new javax.swing.JLabel();
        highGoalAccuracyLabel = new javax.swing.JLabel();
        ballsCaughtFromHPAccuracySlider = new javax.swing.JSlider();
        lowGoalAccuracyLabel = new javax.swing.JLabel();
        lowGoalAccuracySlider = new javax.swing.JSlider();
        ballsCaughtFromHPSpinner = new javax.swing.JSpinner();
        ballsCaughtFromHPLabel = new javax.swing.JLabel();
        highGoalsScoredSpinner = new javax.swing.JSpinner();
        highGoalsScoredLabel = new javax.swing.JLabel();
        lowGoalsScoredSpinner = new javax.swing.JSpinner();
        lowGoalsScoredLabel = new javax.swing.JLabel();
        assistsSpinner = new javax.swing.JSpinner();
        assistsLabel = new javax.swing.JLabel();
        cyclesSpinner = new javax.swing.JSpinner();
        cyclesLabel = new javax.swing.JLabel();
        defensiveLabel = new javax.swing.JLabel();
        defensiveSlider = new javax.swing.JSlider();
        overallPanel = new javax.swing.JPanel();
        showOnlyFromCurrentEvenCheckBox = new javax.swing.JCheckBox();
        matchNumberSpinner = new javax.swing.JSpinner();
        matchNumberLabel = new javax.swing.JLabel();
        teamNumberSpinner = new javax.swing.JSpinner();
        teamNumberLabel = new javax.swing.JLabel();
        matchNumberRadioButton = new javax.swing.JRadioButton();
        teamNumberRadioButton = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        eventButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Matches");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        matchTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Match Number", "Team Number", "Event Name"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        matchTable.getTableHeader().setReorderingAllowed(false);
        matchScrollPanel.setViewportView(matchTable);

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        editButton.setText("Edit");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        updateButton.setText("Update");
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        resetButton.setText("Reset");
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });

        teamButton.setText("Teams");
        teamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teamButtonActionPerformed(evt);
            }
        });

        autoBallCountLabel.setText("Number of Auto Balls");

        autoBallCountSlider.setMajorTickSpacing(1);
        autoBallCountSlider.setMaximum(3);
        autoBallCountSlider.setMinorTickSpacing(1);
        autoBallCountSlider.setPaintLabels(true);
        autoBallCountSlider.setPaintTicks(true);
        autoBallCountSlider.setSnapToTicks(true);
        autoBallCountSlider.setValue(0);

        zoneChangeCheckBox.setText("Changes Zone (Auto)");

        autoBallGoalLabel.setText("Auto Ball Goal");

        autoBallGoalSlider.setMajorTickSpacing(1);
        autoBallGoalSlider.setMaximum(2);
        autoBallGoalSlider.setMinorTickSpacing(1);
        autoBallGoalSlider.setPaintLabels(true);
        autoBallGoalSlider.setPaintTicks(true);
        autoBallGoalSlider.setSnapToTicks(true);
        autoBallGoalSlider.setValue(0);
        Dictionary<Integer, Component> autoGoalLabel = new Hashtable<Integer, Component>();
        autoGoalLabel.put(0, new JLabel("None"));
        autoGoalLabel.put(1, new JLabel("Low"));
        autoGoalLabel.put(2, new JLabel("High"));
        autoBallGoalSlider.setLabelTable(autoGoalLabel);

        ableToUnloadAutoBallCheckBox.setText("Able to Unload (Auto)");

        javax.swing.GroupLayout autonomousPanelLayout = new javax.swing.GroupLayout(autonomousPanel);
        autonomousPanel.setLayout(autonomousPanelLayout);
        autonomousPanelLayout.setHorizontalGroup(
            autonomousPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(autonomousPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(autonomousPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ableToUnloadAutoBallCheckBox)
                    .addComponent(autoBallCountLabel)
                    .addComponent(autoBallCountSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(zoneChangeCheckBox)
                    .addComponent(autoBallGoalLabel)
                    .addComponent(autoBallGoalSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        autonomousPanelLayout.setVerticalGroup(
            autonomousPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(autonomousPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(autoBallCountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(autoBallCountSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(zoneChangeCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(autoBallGoalLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(autoBallGoalSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ableToUnloadAutoBallCheckBox)
                .addContainerGap(306, Short.MAX_VALUE))
        );

        searchTabbedPane.addTab("Autonomous", autonomousPanel);

        teleopScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        ableToTrussTossCheckBox.setText("Can Toss Over Truss");

        floorPickupSlider.setMajorTickSpacing(1);
        floorPickupSlider.setMaximum(5);
        floorPickupSlider.setMinorTickSpacing(1);
        floorPickupSlider.setPaintLabels(true);
        floorPickupSlider.setPaintTicks(true);
        floorPickupSlider.setSnapToTicks(true);
        floorPickupSlider.setValue(0);

        ableToTrussCatchCheckBox.setText("Can Catch from Truss");

        ableToCatchCheckBox.setText("Catches In General");

        floorPickupLabel.setText("Floor Pickup Ability/Speed");

        highGoalAccuracySlider.setMajorTickSpacing(50);
        highGoalAccuracySlider.setPaintLabels(true);
        highGoalAccuracySlider.setPaintTicks(true);
        highGoalAccuracySlider.setValue(0);
        Dictionary<Integer, Component> precentLabel = new Hashtable<Integer, Component>();
        precentLabel.put(0, new JLabel("0%"));
        precentLabel.put(50, new JLabel("50%"));
        precentLabel.put(100, new JLabel("100%"));
        highGoalAccuracySlider.setLabelTable(precentLabel);
        highGoalAccuracySlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                highGoalAccuracySliderStateChanged(evt);
            }
        });

        ballsCaughtFromHPAccuracyLabel.setText("Balls Caught From HP Accuracy");

        highGoalAccuracyLabel.setText("High Goal Accuracy");

        ballsCaughtFromHPAccuracySlider.setMajorTickSpacing(50);
        ballsCaughtFromHPAccuracySlider.setMinorTickSpacing(99);
        ballsCaughtFromHPAccuracySlider.setPaintLabels(true);
        ballsCaughtFromHPAccuracySlider.setPaintTicks(true);
        ballsCaughtFromHPAccuracySlider.setValue(0);
        ballsCaughtFromHPAccuracySlider.setLabelTable(precentLabel);
        ballsCaughtFromHPAccuracySlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                ballsCaughtFromHPAccuracySliderStateChanged(evt);
            }
        });

        lowGoalAccuracyLabel.setText("Low Goal Accuracy");

        lowGoalAccuracySlider.setMajorTickSpacing(50);
        lowGoalAccuracySlider.setPaintLabels(true);
        lowGoalAccuracySlider.setPaintTicks(true);
        lowGoalAccuracySlider.setValue(0);
        lowGoalAccuracySlider.setLabelTable(precentLabel);
        lowGoalAccuracySlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                lowGoalAccuracySliderStateChanged(evt);
            }
        });

        ballsCaughtFromHPSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        ballsCaughtFromHPLabel.setText("Balls Caught From HP");

        highGoalsScoredSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        highGoalsScoredLabel.setText("High Goals Scored");

        lowGoalsScoredSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        lowGoalsScoredLabel.setText("Low Goals Scored");

        assistsSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        assistsLabel.setText("Assists");

        cyclesSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        cyclesLabel.setText("Scoring Cycles");

        defensiveLabel.setText("Defensive Capibilites");

        defensiveSlider.setMajorTickSpacing(1);
        defensiveSlider.setMaximum(5);
        defensiveSlider.setPaintLabels(true);
        defensiveSlider.setPaintTicks(true);
        defensiveSlider.setToolTipText("");
        defensiveSlider.setValue(0);

        javax.swing.GroupLayout teleopPanelLayout = new javax.swing.GroupLayout(teleopPanel);
        teleopPanel.setLayout(teleopPanelLayout);
        teleopPanelLayout.setHorizontalGroup(
            teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(teleopPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ableToCatchCheckBox)
                    .addComponent(ableToTrussCatchCheckBox)
                    .addComponent(ableToTrussTossCheckBox)
                    .addComponent(floorPickupLabel)
                    .addComponent(floorPickupSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(highGoalAccuracySlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ballsCaughtFromHPAccuracyLabel)
                    .addComponent(highGoalAccuracyLabel)
                    .addComponent(ballsCaughtFromHPAccuracySlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lowGoalAccuracyLabel)
                    .addComponent(lowGoalAccuracySlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(teleopPanelLayout.createSequentialGroup()
                        .addGroup(teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cyclesSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(assistsSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lowGoalsScoredSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(highGoalsScoredSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ballsCaughtFromHPSpinner, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ballsCaughtFromHPLabel)
                            .addComponent(highGoalsScoredLabel)
                            .addComponent(lowGoalsScoredLabel)
                            .addComponent(assistsLabel)
                            .addComponent(cyclesLabel)))
                    .addComponent(defensiveLabel)
                    .addComponent(defensiveSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        teleopPanelLayout.setVerticalGroup(
            teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(teleopPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ableToTrussTossCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ableToTrussCatchCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ableToCatchCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(floorPickupLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(floorPickupSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(defensiveLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(defensiveSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ballsCaughtFromHPAccuracyLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ballsCaughtFromHPAccuracySlider, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(highGoalAccuracyLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(highGoalAccuracySlider, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lowGoalAccuracyLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lowGoalAccuracySlider, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ballsCaughtFromHPSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ballsCaughtFromHPLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(highGoalsScoredSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(highGoalsScoredLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lowGoalsScoredSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lowGoalsScoredLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(assistsSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(assistsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(teleopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cyclesSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cyclesLabel))
                .addGap(20, 20, 20))
        );

        teleopScrollPane.setViewportView(teleopPanel);

        searchTabbedPane.addTab("Tele-Op", teleopScrollPane);

        showOnlyFromCurrentEvenCheckBox.setSelected(true);
        showOnlyFromCurrentEvenCheckBox.setText("Matched Only From Current Event");

        matchNumberSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        matchNumberLabel.setText("Match Number");

        teamNumberSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        teamNumberLabel.setText("Team Number");

        sortingButtonGroup.add(matchNumberRadioButton);
        matchNumberRadioButton.setSelected(true);
        matchNumberRadioButton.setText("Match Number");

        sortingButtonGroup.add(teamNumberRadioButton);
        teamNumberRadioButton.setText("Team Number");

        jLabel2.setText("Sort By:");

        javax.swing.GroupLayout overallPanelLayout = new javax.swing.GroupLayout(overallPanel);
        overallPanel.setLayout(overallPanelLayout);
        overallPanelLayout.setHorizontalGroup(
            overallPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(overallPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(overallPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(teamNumberRadioButton)
                    .addComponent(jLabel2)
                    .addComponent(matchNumberRadioButton)
                    .addComponent(showOnlyFromCurrentEvenCheckBox)
                    .addGroup(overallPanelLayout.createSequentialGroup()
                        .addGroup(overallPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(matchNumberSpinner, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                            .addComponent(teamNumberSpinner))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(overallPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(teamNumberLabel)
                            .addComponent(matchNumberLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        overallPanelLayout.setVerticalGroup(
            overallPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(overallPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(showOnlyFromCurrentEvenCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(overallPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(matchNumberSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(matchNumberLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(overallPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(teamNumberSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(teamNumberLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(matchNumberRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(teamNumberRadioButton)
                .addContainerGap(335, Short.MAX_VALUE))
        );

        searchTabbedPane.addTab("Overall", overallPanel);

        eventButton.setText("Events");
        eventButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eventButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(resetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(searchTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(matchScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(teamButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(eventButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(deleteButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                    .addComponent(editButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                    .addComponent(addButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(addButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton)
                        .addGap(64, 64, 64)
                        .addComponent(teamButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eventButton)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(matchScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(resetButton)
                            .addComponent(updateButton))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        new TeamEventMatchInput().display().setLocationRelativeTo(this);
    }//GEN-LAST:event_addButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        new TeamEventMatchInput((TeamEventMatch) matchTable.getValueAt(matchTable.getSelectedRow(), 0)).display().setLocationRelativeTo(this);
    }//GEN-LAST:event_editButtonActionPerformed

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        updateTEMModel();
    }//GEN-LAST:event_formWindowGainedFocus

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        TeamEventMatchHandler.deleteEvent((TeamEventMatch) matchTable.getValueAt(matchTable.getSelectedRow(), 0));
        updateTEMModel();
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        updateTEMModel();
    }//GEN-LAST:event_updateButtonActionPerformed

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
        resetSearch();
        updateTEMModel();
    }//GEN-LAST:event_resetButtonActionPerformed

    private void teamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teamButtonActionPerformed
        new TeamViewer().display().setLocationRelativeTo(this);
    }//GEN-LAST:event_teamButtonActionPerformed

    private void eventButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eventButtonActionPerformed
        new EventViewer().display().setLocationRelativeTo(this);
    }//GEN-LAST:event_eventButtonActionPerformed

    private void ballsCaughtFromHPAccuracySliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_ballsCaughtFromHPAccuracySliderStateChanged
        ballsCaughtFromHPAccuracySlider.setToolTipText(getHumanPlayerAccuracy() * 100 + "%");
    }//GEN-LAST:event_ballsCaughtFromHPAccuracySliderStateChanged

    private void highGoalAccuracySliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_highGoalAccuracySliderStateChanged
        highGoalAccuracySlider.setToolTipText(getHighGoalAccuracy() * 100 + "%");
    }//GEN-LAST:event_highGoalAccuracySliderStateChanged

    private void lowGoalAccuracySliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_lowGoalAccuracySliderStateChanged
        lowGoalAccuracySlider.setToolTipText(getLowGoalAccuracy() * 100 + "%");
    }//GEN-LAST:event_lowGoalAccuracySliderStateChanged

    /**
     * Updates the table's model with the all the matches that match the search
     */
    private void updateTEMModel() {
        List matchesShowing = getMatches();
        DefaultTableModel model;

        model = (DefaultTableModel) matchTable.getModel();
        int rows = matchTable.getRowCount();

        for (int i = rows - 1; i >= 0; i--) {
            model.removeRow(i);
        }
        if (teamNumberRadioButton.isSelected()) {
            teamQuicksort(matchesShowing, 0, matchesShowing.size() - 1);
        }
        for (int i = 0; i < matchesShowing.size(); i++) {
            TeamEventMatch tem = (TeamEventMatch) matchesShowing.get(i);
            model.addRow(new Object[]{tem, tem.getTeamNumber(), EventHandler.getEventFromId(tem.getEventID())});
        }
    }

    /**
     * Returns all the matches that match the search
     *
     * @return all the matches that match the search
     */
    public List getMatches() {
        List allMatches;
        if (getEventID() != 0) {
            allMatches = TeamEventMatchHandler.getMatchesFromEvent(EventHandler.getCurrentEvent().getEventID());
        } else {
            allMatches = TeamEventMatchHandler.getMatches();
        }
        List foundMatches = new ArrayList();

        for (int i = 0; i < allMatches.size(); i++) {
            TeamEventMatch currentMatch = (TeamEventMatch) allMatches.get(i);

            if (getMatchNumber() != 0) {
                if (currentMatch.getMatchNumber() != getMatchNumber()) {
                    continue;
                }
            }
            if (getTeamNumber() != 0) {
                if (currentMatch.getTeamNumber() != getTeamNumber()) {
                    continue;
                }
            }

            if (isAbleToTrussCatch()) {
                if (!currentMatch.isAbleToTrussCatch()) {
                    continue;
                }
            }
            if (isAbleToTrussToss()) {
                if (!currentMatch.isAbleToTrussToss()) {
                    continue;
                }
            }
            if (isAbleToUnloadAutoBall()) {
                if (!currentMatch.isAbleToUnloadAutoBall()) {
                    continue;
                }
            }
            if (isZoneChange()) {
                if (!currentMatch.isZoneChange()) {
                    continue;
                }
            }
            
            if (getAutoBallGoal() != 0){
                if (getAutoBallGoal() != currentMatch.getAutoBallGoal()){
                    continue;
                }
            }

            if (currentMatch.getAssists() < getAssists()) {
                continue;
            }

            if (currentMatch.getAutoBallCount() < getAutoBallCount()) {
                continue;
            }
            if (currentMatch.getBallsCaughtFromHP() < getBallsCaughtFromHP()) {
                continue;
            }
            if (currentMatch.getCycles() < getCycles()) {
                continue;
            }
            if (currentMatch.getDefensive() < getDefensive()) {
                continue;
            }
            if (currentMatch.getFloorPickup() < getFloorPickup()) {
                continue;
            }
            if (currentMatch.getHighGoalAccuracy() < getHighGoalAccuracy()) {
                continue;
            }
            if (currentMatch.getHighGoalsScored() < getHighGoalsScored()) {
                continue;
            }
            if (currentMatch.getHumanPlayerAccuracy() < getHumanPlayerAccuracy()) {
                continue;
            }
            if (currentMatch.getLowGoalAccuracy() < getLowGoalAccuracy()) {
                continue;
            }
            if (currentMatch.getLowGoalsScored() < getLowGoalsScored()) {
                continue;
            }
            foundMatches.add(currentMatch);

        }
        return foundMatches;
    }

    /**
     * Sets all the search items to 0 or unchecked
     */
    private void resetSearch() {
        ableToCatchCheckBox.setSelected(false);
        ableToTrussCatchCheckBox.setSelected(false);
        ableToTrussTossCheckBox.setSelected(false);
        ableToUnloadAutoBallCheckBox.setSelected(false);
        assistsSpinner.setValue(0);
        autoBallCountSlider.setValue(0);
        autoBallGoalSlider.setValue(0);
        ballsCaughtFromHPAccuracySlider.setValue(0);
        ballsCaughtFromHPSpinner.setValue(0);
        cyclesSpinner.setValue(0);
        defensiveSlider.setValue(0);
        floorPickupSlider.setValue(0);
        highGoalAccuracySlider.setValue(0);
        highGoalsScoredSpinner.setValue(0);
        lowGoalAccuracySlider.setValue(0);
        lowGoalsScoredSpinner.setValue(0);
        matchNumberSpinner.setValue(0);
        //showOnlyFromCurrentEvenCheckBox.setSelected(false);
        teamNumberSpinner.setValue(0);
        zoneChangeCheckBox.setSelected(false);
    }

    private int partitionByTeam(List<TeamEventMatch> matches, int start, int end) {
        int pivot = matches.get(start).getTeamNumber();

        while (start < end) {

            while (matches.get(start).getTeamNumber() < pivot) {
                start++;
            }

            while (matches.get(end).getTeamNumber() > pivot) {
                end--;
            }

            if (matches.get(start).getTeamNumber() == matches.get(end).getTeamNumber()) {
                if (matches.get(start).getMatchNumber() > matches.get(end).getMatchNumber()) {
                    TeamEventMatch temp = matches.get(start);
                    matches.set(start, matches.get(end));
                    matches.set(end, temp);
                }
                end--;

            } else if (start < end) {
                TeamEventMatch temp = matches.get(start);
                matches.set(start, matches.get(end));
                matches.set(end, temp);
            }

        }

        return start;
    }

    private void teamQuicksort(List matches, int start, int end) {
        if (start < end) {

            int middle = partitionByTeam(matches, start, end);
            teamQuicksort(matches, start, middle - 1);
            teamQuicksort(matches, middle + 1, end);
        }

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox ableToCatchCheckBox;
    private javax.swing.JCheckBox ableToTrussCatchCheckBox;
    private javax.swing.JCheckBox ableToTrussTossCheckBox;
    private javax.swing.JCheckBox ableToUnloadAutoBallCheckBox;
    private javax.swing.JButton addButton;
    private javax.swing.JLabel assistsLabel;
    private javax.swing.JSpinner assistsSpinner;
    private javax.swing.JLabel autoBallCountLabel;
    private javax.swing.JSlider autoBallCountSlider;
    private javax.swing.JLabel autoBallGoalLabel;
    private javax.swing.JSlider autoBallGoalSlider;
    private javax.swing.JPanel autonomousPanel;
    private javax.swing.JLabel ballsCaughtFromHPAccuracyLabel;
    private javax.swing.JSlider ballsCaughtFromHPAccuracySlider;
    private javax.swing.JLabel ballsCaughtFromHPLabel;
    private javax.swing.JSpinner ballsCaughtFromHPSpinner;
    private javax.swing.JLabel cyclesLabel;
    private javax.swing.JSpinner cyclesSpinner;
    private javax.swing.JLabel defensiveLabel;
    private javax.swing.JSlider defensiveSlider;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton editButton;
    private javax.swing.JButton eventButton;
    private javax.swing.JLabel floorPickupLabel;
    private javax.swing.JSlider floorPickupSlider;
    private javax.swing.JLabel highGoalAccuracyLabel;
    private javax.swing.JSlider highGoalAccuracySlider;
    private javax.swing.JLabel highGoalsScoredLabel;
    private javax.swing.JSpinner highGoalsScoredSpinner;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lowGoalAccuracyLabel;
    private javax.swing.JSlider lowGoalAccuracySlider;
    private javax.swing.JLabel lowGoalsScoredLabel;
    private javax.swing.JSpinner lowGoalsScoredSpinner;
    private javax.swing.JLabel matchNumberLabel;
    private javax.swing.JRadioButton matchNumberRadioButton;
    private javax.swing.JSpinner matchNumberSpinner;
    private javax.swing.JScrollPane matchScrollPanel;
    private javax.swing.JTable matchTable;
    private javax.swing.JPanel overallPanel;
    private javax.swing.JButton resetButton;
    private javax.swing.JTabbedPane searchTabbedPane;
    private javax.swing.JCheckBox showOnlyFromCurrentEvenCheckBox;
    private javax.swing.ButtonGroup sortingButtonGroup;
    private javax.swing.JButton teamButton;
    private javax.swing.JLabel teamNumberLabel;
    private javax.swing.JRadioButton teamNumberRadioButton;
    private javax.swing.JSpinner teamNumberSpinner;
    private javax.swing.JPanel teleopPanel;
    private javax.swing.JScrollPane teleopScrollPane;
    private javax.swing.JButton updateButton;
    private javax.swing.JCheckBox zoneChangeCheckBox;
    // End of variables declaration//GEN-END:variables

    private int getAutoBallCount() {
        return autoBallCountSlider.getValue();
    }

    private boolean isZoneChange() {
        return zoneChangeCheckBox.isSelected();
    }

    private int getAutoBallGoal() {
        return autoBallGoalSlider.getValue();
    }

    private boolean isAbleToUnloadAutoBall() {
        return ableToUnloadAutoBallCheckBox.isSelected();
    }

    private boolean isAbleToTrussToss() {
        return ableToTrussTossCheckBox.isSelected();
    }

    private boolean isAbleToTrussCatch() {
        return ableToTrussCatchCheckBox.isSelected();
    }

    private boolean isAbleToCatch() {
        return ableToCatchCheckBox.isSelected();
    }

    private int getFloorPickup() {
        return floorPickupSlider.getValue();
    }

    private double getHumanPlayerAccuracy() {
        return ballsCaughtFromHPAccuracySlider.getValue() / 100.0;
    }

    private double getHighGoalAccuracy() {
        return highGoalAccuracySlider.getValue() / 100.0;
    }

    private double getLowGoalAccuracy() {
        return lowGoalAccuracySlider.getValue() / 100.0;
    }

    private int getBallsCaughtFromHP() {
        return (Integer) ballsCaughtFromHPSpinner.getValue();
    }

    private int getHighGoalsScored() {
        return (Integer) highGoalsScoredSpinner.getValue();
    }

    private int getLowGoalsScored() {
        return (Integer) lowGoalsScoredSpinner.getValue();
    }

    private int getAssists() {
        return (Integer) assistsSpinner.getValue();
    }

    private int getDefensive() {
        return defensiveSlider.getValue();
    }

    private int getCycles() {
        return (Integer) cyclesSpinner.getValue();
    }

    private int getEventID() {
        if (showOnlyFromCurrentEvenCheckBox.isSelected()) {
            return EventHandler.getCurrentEvent().getEventID();
        } else {
            return 0;
        }
    }

    private int getMatchNumber() {
        return (Integer) matchNumberSpinner.getValue();
    }

    private int getTeamNumber() {
        return (Integer) teamNumberSpinner.getValue();
    }
}
